{% extends 'base.html' %}
{% load static %}

{% block title %}设备详情 - {{ asset.name }} - 维修管理系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1>设备详情</h1>
                <div>
                    <a href="{% url 'assets:asset_edit' asset.pk %}" class="btn btn-outline-primary me-2">
                        <i class="bi bi-pencil"></i> 编辑
                    </a>
                    <a href="{% url 'assets:asset_list_mine' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-arrow-left"></i> 返回列表
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- 设备基本信息 -->
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="card-title mb-0">基本信息</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-6 mb-2">
                            <strong>设备名称:</strong><br>
                            {{ asset.name }}
                        </div>
                        <div class="col-6 mb-2">
                            <strong>设备型号:</strong><br>
                            {{ asset.model|default:"未设置" }}
                        </div>
                        <div class="col-6 mb-2">
                            <strong>设备类型:</strong><br>
                            {{ asset.get_asset_type_display }}
                        </div>
                        <div class="col-6 mb-2">
                            <strong>序列号:</strong><br>
                            {{ asset.serial_number|default:"未设置" }}
                        </div>
                        <div class="col-6 mb-2">
                            <strong>设备状态:</strong><br>
                            <span class="badge bg-{% if asset.status == 'active' %}success{% elif asset.status == 'maintenance' %}warning{% else %}secondary{% endif %}">
                                {{ asset.get_status_display }}
                            </span>
                        </div>
                        <div class="col-6 mb-2">
                            <strong>购买日期:</strong><br>
                            {{ asset.purchase_date|date:"Y-m-d"|default:"未设置" }}
                        </div>
                        <div class="col-6 mb-2">
                            <strong>购买价格:</strong><br>
                            {% if asset.purchase_price %}
                                ¥{{ asset.purchase_price }}
                            {% else %}
                                未设置
                            {% endif %}
                        </div>
                        <div class="col-6 mb-2">
                            <strong>所属部门:</strong><br>
                            {{ asset.department|default:"未设置" }}
                        </div>
                        <div class="col-12 mb-2">
                            <strong>设备编码:</strong><br>
                            {{ asset.code }}
                        </div>
                        {% if asset.notes %}
                        <div class="col-12 mb-2">
                            <strong>备注:</strong><br>
                            {{ asset.notes }}
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- 状态更新表单 -->
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">更新状态</h5>
                </div>
                <div class="card-body">
                    <form method="post" action="{% url 'assets:asset_update_status' asset.pk %}">
                        {% csrf_token %}
                        <div class="mb-3">
                            <label for="status" class="form-label">选择新状态</label>
                            <select name="status" id="status" class="form-select">
                                <option value="active" {% if asset.status == 'active' %}selected{% endif %}>正常</option>
                                <option value="maintenance" {% if asset.status == 'maintenance' %}selected{% endif %}>维修中</option>
                                <option value="retired" {% if asset.status == 'retired' %}selected{% endif %}>已报废</option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-arrow-repeat"></i> 更新状态
                        </button>
                    </form>
                </div>
            </div>
        </div>

        <!-- 维修历史 -->
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">维修历史</h5>
                </div>
                <div class="card-body">
                    {% if maintenance_history %}
                    <div class="list-group list-group-flush">
                        {% for ticket in maintenance_history %}
                        <div class="list-group-item">
                            <div class="d-flex w-100 justify-content-between">
                                <h6 class="mb-1">{{ ticket.title }}</h6>
                                <small class="text-success">已完成</small>
                            </div>
                            <p class="mb-1 text-muted small">{{ ticket.description|truncatewords:10 }}</p>
                            <small class="text-muted">
                                完成时间: {{ ticket.completed_at|date:"Y-m-d H:i" }} |
                                处理人: {{ ticket.assigned_to.get_full_name|default:ticket.assigned_to.username }}
                            </small>
                        </div>
                        {% endfor %}
                    </div>
                    {% else %}
                    <div class="text-center text-muted py-4">
                        <i class="bi bi-wrench" style="font-size: 2rem;"></i>
                        <p class="mt-2">暂无维修记录</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
